import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class MemberLevelService extends BaseService<any> {
  private readonly logger = new Logger(MemberLevelService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * getList
   * 对应 PHP: MemberLevelService_api::getList()
   * 逻辑类型: undefined - undefined
   */
  async getList() {
    // 基于PHP真实逻辑: getList
    // PHP原文: $list = (new MemberLevel())->where([ ['site_id', '=', $this->site_id] ])->field('level_id,site_id,level_name,growth,remark,level_benefits,level_gifts'...
const list = this.memberLevelService.where([ ['site_id', '=', this.site_id) )).field('level_id,site_id,level_name,growth,remark,level_benefits,level_gifts').order('growth asc').select().toArray(];
        const level_style = MemberLevelDict.getStyle();
        if (!!list)) {
            foreach (list as k => item) {
                if (!!item.level_benefits)) list[k].level_benefits = array_filter(array_map(function (item){
                    if (item.content) return item;
                }, CoreMemberService.getBenefitsContent(item.site_id, item.level_benefits, 'member_level'))];
                if (!!item.level_gifts)) list[k].level_gifts = CoreMemberService.getGiftContent(item.site_id, item.level_gifts, 'member_level'];

                const filling = [
                    benefits_one: [ title: "专属客服",  desc: '专业服务', icon: '/static/resource/images/member/benefits/benefits_kefu.png' ],
                    benefits_two: [ title: "专属徽章",  desc: '专属徽章', icon: '/static/resource/images/member/benefits/benefits_badge.png' ],
                    benefits_four: [ title: "经验累计",  desc: '经验累计', icon: '/static/resource/images/member/benefits/benefits_experience.png' ],
                    benefits_three: [ title: "尊享客服",  desc: '尊享客服', icon: '/static/resource/images/member/benefits/benefits_badge.png' ],
                );

                const length = !item.level_benefits) ? 0 : count(item.level_benefits);
                if (length < 4) {
                    if (!item.level_benefits)) list[k].level_benefits = [];
                    foreach (filling as key => content) {
                        if (count(list[k).level_benefits) == 4) break;
                        list[k].level_benefits[key] = [
                            content: content
                        ;
  }
                const level_key = k % 7 + 1;
                list[k].level_bg = '/static/resource/images/member/level/bg_'. level_key  + '.png';
                list[k].member_bg = '/static/resource/images/member/level/member_'. level_key  + '.png';
                list[k].level_icon = '/static/resource/images/member/level/level_icon'. level_key  + '.png';
                list[k].level_tag = '/static/resource/images/member/level/level_'. level_key  + '.png';

                list[k].level_style = level_style['level_'.level_key] || [];

            }
  }
}
